package com.portico.dept.solarch

@grails.plugins.springsecurity.Secured(['ROLE_SA_USER'])
class ProjectRisksController {

  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  static scaffold = true;

  def index = {
    redirect(action: "list", params: params)
  }

  def list = {
    params.max = Math.min(params.max ? params.int('max') : 10, 100)
    [projectRisksInstanceList: ProjectRisks.list(params), projectRisksInstanceTotal: ProjectRisks.count()]
  }

  def create = {
    def projectRisksInstance = new ProjectRisks()
    projectRisksInstance.properties = params
    return [projectRisksInstance: projectRisksInstance]
  }

  def save = {
    def projectRisksInstance = new ProjectRisks(params)
    if (projectRisksInstance.save(flush: true)) {
      flash.message = "${message(code: 'default.created.message', args: [message(code: 'projectRisks.label', default: 'ProjectRisks'), projectRisksInstance.id])}"
      redirect(action: "show", id: projectRisksInstance.id)
    }
    else {
      render(view: "create", model: [projectRisksInstance: projectRisksInstance])
    }
  }

  def show = {
    def projectRisksInstance = ProjectRisks.get(params.id)
    if (!projectRisksInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'projectRisks.label', default: 'ProjectRisks'), params.id])}"
      redirect(action: "list")
    }
    else {
      [projectRisksInstance: projectRisksInstance]
    }
  }

  def edit = {
    def projectRisksInstance = ProjectRisks.get(params.id)
    if (!projectRisksInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'projectRisks.label', default: 'ProjectRisks'), params.id])}"
      redirect(action: "list")
    }
    else {
      return [projectRisksInstance: projectRisksInstance]
    }
  }

  def update = {
    def projectRisksInstance = ProjectRisks.get(params.id)
    if (projectRisksInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (projectRisksInstance.version > version) {

          projectRisksInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'projectRisks.label', default: 'ProjectRisks')] as Object[], "Another user has updated this ProjectRisks while you were editing")
          render(view: "edit", model: [projectRisksInstance: projectRisksInstance])
          return
        }
      }
      projectRisksInstance.properties = params
      if (!projectRisksInstance.hasErrors() && projectRisksInstance.save(flush: true)) {
        flash.message = "${message(code: 'default.updated.message', args: [message(code: 'projectRisks.label', default: 'ProjectRisks'), projectRisksInstance.id])}"
        redirect(action: "show", id: projectRisksInstance.id)
      }
      else {
        render(view: "edit", model: [projectRisksInstance: projectRisksInstance])
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'projectRisks.label', default: 'ProjectRisks'), params.id])}"
      redirect(action: "list")
    }
  }

  def delete = {
    def projectRisksInstance = ProjectRisks.get(params.id)
    if (projectRisksInstance) {
      try {
        projectRisksInstance.delete(flush: true)
        flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'projectRisks.label', default: 'ProjectRisks'), params.id])}"
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'projectRisks.label', default: 'ProjectRisks'), params.id])}"
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'projectRisks.label', default: 'ProjectRisks'), params.id])}"
      redirect(action: "list")
    }
  }
}
